﻿#include "Pass.h"


Pass::Pass()
{
}

void Pass::AddSubPass(std::shared_ptr<Pass> subPass)
{
    assert(subPass);
    mSubPass.push_back(subPass);
}

void Pass::DrawRenderItem(
    ID3D12GraphicsCommandList* cmdList,
    CD3DX12_GPU_DESCRIPTOR_HANDLE hGpuHandler,
    UINT srvHeapSize,
    UINT objCBByteSize,
    UINT matCBByteSize,
    ID3D12Resource* objectCB,
    ID3D12Resource* matCB,
    const std::vector<RenderItem*>& ritems
    )
{
    for(size_t i = 0 ; i < ritems.size(); ++i)
    {
        auto ri = ritems[i];
        auto vertexView = ri->Geo->VertexBufferView();
        auto indexView = ri->Geo->IndexBufferView();
		
        cmdList->IASetVertexBuffers(0,1,&vertexView);
        cmdList->IASetIndexBuffer(&indexView);
        cmdList->IASetPrimitiveTopology(ri->PrimitiveType);
        
        hGpuHandler.Offset(ri->Mat->DiffuseSrvHeapIndex,srvHeapSize);

        D3D12_GPU_VIRTUAL_ADDRESS objCBAddress = objectCB->GetGPUVirtualAddress() + ri->ObjCBIndex*objCBByteSize;
        D3D12_GPU_VIRTUAL_ADDRESS matCBAddress = matCB->GetGPUVirtualAddress() + ri->Mat->MatCBIndex*matCBByteSize;

        cmdList->SetGraphicsRootDescriptorTable(0,hGpuHandler);
        cmdList->SetGraphicsRootConstantBufferView(1, objCBAddress);
        cmdList->SetGraphicsRootConstantBufferView(3, matCBAddress);
		
        cmdList->DrawIndexedInstanced(ri->IndexCount,1,ri->StartIndexLocation,ri->BaseVertexLocation,0);
    }
}


